/*
 * This file is part of aion-unique <aion-unique.com>.
 *
 *  aion-unique is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  aion-unique is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with aion-unique.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionemu.gameserver.network.aion.clientpackets;

import org.apache.log4j.Logger;

import com.aionemu.gameserver.model.gameobjects.AionObject;
import com.aionemu.gameserver.model.gameobjects.VisibleObject;
import com.aionemu.gameserver.model.gameobjects.interfaces.IDialogRequest;
import com.aionemu.gameserver.model.gameobjects.player.Player;
import com.aionemu.gameserver.network.aion.AbstractAionClientPacket;
import com.aionemu.gameserver.world.World;

/**
 * 
 * @author Lyahim, alexa026, Avol
 * modified by ATracer
 * 
 */
public class CM_SHOW_DIALOG extends AbstractAionClientPacket
{
	private static final Logger	log	= Logger.getLogger(CM_SHOW_DIALOG.class);
	private int					targetObjectId;

	/**
	 * Constructs new instance of <tt>CM_SHOW_DIALOG </tt> packet
	 * @param opcode
	 */
	public CM_SHOW_DIALOG(int opcode)
	{
		super(opcode);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void readImpl()
	{
		targetObjectId = readD();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void runImpl()
	{
		AionObject targetObject = World.getInstance().findAionObject(targetObjectId);
		Player player = getClient().getActivePlayer();

		if (targetObject == null)
		{
			log.error("targetObject == null" + "targetObjectId: " + targetObjectId);
			return;
		}

		if (player == null)
		{
			log.error("player == null");
			return;
		}
		if (targetObject instanceof IDialogRequest)
		{
			((VisibleObject) targetObject).setTarget(player);
			((IDialogRequest) targetObject).onDialogRequest(player);
		}
	}

	@Override
	public String toString()
	{
		return "CM_SHOW_DIALOG [targetObjectId=" + targetObjectId + "]";
	}
}